package com.haoxi.gateway.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: QianXM
 * Date: 2006-2-16
 */
public final class InitSystemControlConfig {

    private static Log log = LogFactory.getLog ( InitSystemControlConfig.class );
    private static Properties props = new Properties ();

    /**
     * 动态加载系统控制参数
     */
    public static Map loadSystemControlConfig() {
        Map systemControlConf = new HashMap();
        InputStream in = InitSystemControlConfig.class.getResourceAsStream ( "/systemConfig.properties" );
        if ( in != null ) {
            try {
                // 同步锁主props
                synchronized (props) {
                    props.load ( in );
                }
                in.close ();
            }
            catch ( Exception e ) {
                log.fatal ( "error occurs during load system configuration...", e );
                throw new RuntimeException ( e.getMessage () );
            }
            
            String gateway_sms = InitSystemControlConfig.getString(SYSConst.CONF_DB_GATEWAY);
            String gateway_history = InitSystemControlConfig.getString(SYSConst.CONF_DB_HISTORY);
            String gateway_mysql = InitSystemControlConfig.getString(SYSConst.CONF_DB_MYSQL);

            int submitSpeed = InitSystemControlConfig.getInt(SYSConst.CONF_SUBMIT_SPEED);
            int getMTSpeed = InitSystemControlConfig.getInt(SYSConst.CONF_GETMT_SPEED);
            int dealReportSpeed = InitSystemControlConfig.getInt(SYSConst.CONF_DEALREPORT_SPEED);
            
            systemControlConf.put(SYSConst.CONF_DB_GATEWAY, gateway_sms);
            systemControlConf.put(SYSConst.CONF_DB_HISTORY, gateway_history);
            systemControlConf.put(SYSConst.CONF_DB_MYSQL, gateway_mysql);
            systemControlConf.put(SYSConst.CONF_SUBMIT_SPEED, new Integer(submitSpeed));
            systemControlConf.put(SYSConst.CONF_GETMT_SPEED, new Integer(getMTSpeed));
            systemControlConf.put(SYSConst.CONF_DEALREPORT_SPEED, new Integer(dealReportSpeed));

            //log.debug("InitSystemControlConfig.loadSystemControlConfig() load successfully! system ControlConf map :["+systemControlConf+"]");
        }
        else {
            log.fatal ( "systemConfig.properties not found. Please make sure the file is in the classpath" );
            throw new RuntimeException ( "systemConfig.properties not found. Please make sure the file is in the classpath" );
        }
        return systemControlConf;
    }
    
    /**
     * Retrieve configuration string by key.
     *
     * @param key
     * @return the value in this property list with the specified key value.
     *         If the key is not found, return an empty string "".
     */
    public static String getString ( String key ) {
        return props.getProperty ( key, "" );
    }

    /**
     * Retrieve configuration int by key.
     *
     * @param key
     * @return the value in this property list with the specified key value
     *          If the key value is not number, return zero.
     */
    public static int getInt ( String key ) {
        int a = 0;
        try {
            a = Integer.parseInt ( props.getProperty ( key ) );
        }
        catch ( NumberFormatException e ) {
            log.error ( "the property value is not number!", e );
            throw new RuntimeException ();
        }

        return a;
    }
}
